package com.dean.dao;

import com.dean.entity.DataFileImpExpEntity;
import com.dean.entity.SysDbConnInfo;
import com.dean.entity.SysMenu;
import com.dean.sort.ComparatorBubbSorter;
import com.dean.sort.MenuOrderNoComparator;
import com.dean.utils.DeanJdbcUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DeanToolDao
 * @Description dean sql lite 脚本操作
 * @Author dean
 * @Date 2021/12/26 10:51
 */
public class DeanToolDao {
    private static volatile DeanToolDao  deanToolDao;
    private DeanToolDao(){}
    public static DeanToolDao getInstance(){
        if(deanToolDao == null){
            synchronized (DeanToolDao.class){
                if(deanToolDao == null){
                    deanToolDao = new DeanToolDao();
                }
            }
        }
        return deanToolDao;
    }

    /**
     * 查询菜单
     * @return
     */
    public List<SysMenu>  queryMenu(){
        List<SysMenu> menus = null;
        String sql = "select * from sys_menu where menu_status = 'Y'";
        List<Map<String,Object>> result =  DeanJdbcUtils.queryForList(sql);
        if(result != null || !result.isEmpty()){
            menus = new ArrayList<SysMenu>();
            for (Map map : result) {
                SysMenu sysMenu = new SysMenu();
                sysMenu.set(map);
                menus.add(sysMenu);
            }
            // sort
            ComparatorBubbSorter<SysMenu> sorter = new ComparatorBubbSorter<SysMenu>();
            sorter.sort(menus, new MenuOrderNoComparator());
        }
        return menus;
    }

    public List<SysDbConnInfo> queryConnInfo(){
        List<SysDbConnInfo> connInfos = new ArrayList<SysDbConnInfo>();
        String sql = "select * from sys_db_connection";
        List<Map<String,Object>> result =  DeanJdbcUtils.queryForList(sql);
        // map to entity
        result.forEach(map->{
            SysDbConnInfo connInfo = new SysDbConnInfo();
            connInfo.set(map);
            connInfos.add(connInfo);
        });
        return connInfos;
    }

    public SysDbConnInfo queryConnInfo(String connName){
        String sql = "select * from sys_db_connection where conn_name = ?";
        Map<String,Object> result =  DeanJdbcUtils.queryForMap(sql,connName);
        SysDbConnInfo connInfo = new SysDbConnInfo();
        connInfo.set(result);
        return connInfo;
    }

    /**
     * 删除连接信息
     * @param connName
     * @return
     */
    public boolean delConnInfo(String connName){
        String sql = "delete from sys_db_connection where conn_name = ?";
        return DeanJdbcUtils.execute(sql,connName);
    }

    /**
     * 保存连接信息
     * @param connInfo
     * @return
     */
    public boolean saveConnInfo(SysDbConnInfo connInfo){
        String sql = "INSERT INTO SYS_DB_CONNECTION(CONN_NAME, DRIVER, URL, USERNAME, PASSWORD) VALUES (?, ?, ?, ?, ?)";
        return DeanJdbcUtils.execute(sql,connInfo.getConnName(),connInfo.getDriver(),connInfo.getUrl(),connInfo.getUsername(),connInfo.getPassword());
    }

    /**
     *
     * @param entity
     * @return
     */
    public boolean saveDataFileImpExpCfg(DataFileImpExpEntity entity){
        String sql = "insert into sys_data_file_imp_exp (conn_name, opt_type, file_type, file_path, commit_num, table_name, delimiter,exe_cfg, data_id)\n" +
                "values (?,?,?,?,?,?,?,?,?);";
        return DeanJdbcUtils.execute(sql,entity.getConnName(),entity.getOptType(),entity.getFileType(),entity.getFilePath(),entity.getCommitNum(),entity.getTableName(),entity.getDelimiter(),entity.getExeCfg(),entity.getDataId());
    }

    public boolean delDataFileImpExpCfg(DataFileImpExpEntity entity){
        String sql = "delete from sys_data_file_imp_exp where data_id = ?";
        return DeanJdbcUtils.execute(sql,entity.getDataId());
    }
    public boolean updateDataFileImpExpCfg(DataFileImpExpEntity entity){
        return delDataFileImpExpCfg(entity)&&saveDataFileImpExpCfg(entity);
    }
}
